<script setup>
import {ref} from 'vue'
import router from "@/router/index.js";
import {userLoginService} from "@/api/user.js";
import {useUserStore} from "@/stores/index.js";
import { ElMessage } from 'element-plus';

const loginRef = ref()
const loginForm = ref({
  username: 'admin',
  password: '123456'
})
const rules = {
  username: [
    {required: true, message: '请输入账户', trigger: 'blur'},
    {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {min: 3, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'}
  ]
}
const login = async () => {
  await loginRef.value.validate();
  const res = await userLoginService(loginForm.value);
  await useUserStore().setUser(res);
  ElMessage.success('登录成功')
  await router.push('/index')
}
</script>

<template>
  <el-container>
    <el-card>
      <el-form ref="loginRef" :model="loginForm" :rules="rules">

          <h2>电商管理后台登录</h2>

        <el-form-item label="账户" prop="username">
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码"  prop="password">
          <el-input type="password" @keyup.enter="login" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"  @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </el-container>

</template>

<style lang="scss" scoped>
.el-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;

  .el-card {
    width: 400px;
    border-radius: 20px;
    padding: 20px;
    transform: translateY(-50%);

    .el-form {
      height: 100%;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      align-content: center;

      .el-form-item {
        margin: 10px 0;

      }

      h2{
        text-align: center;
        margin: 10px 0;
      }
      .el-button {
        width: 100%;
      }
    }


  }
}

</style>
